package com.game.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.game.pojo.vo.Result;
import com.game.pojo.po.User;
import com.game.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvhao
 * @since 2020-07-25
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
@Api(tags = "用户")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/users")
    @ApiOperation("获取用户列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "未知错误")
    })
    public Result getAll(){
        try{
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            return new Result(200,"查询成功",userService.list(queryWrapper));
        }catch (Exception e){
            return new Result(500,"未知错误","服务器发生错误");
        }
    }

    @GetMapping("/user/{id}")
    @ApiOperation("根据id查询")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 202, message = "查询失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result getById(@PathVariable("id") int id){
        try{
            if(userService.getById(id) != null){
                return new Result(200,"查询成功",userService.getById(id));
            }else{
                return new Result(202,"查询失败","id:" + id + "记录不存在");
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }

    @GetMapping("/user/login_check/{id}")
    @ApiOperation("登录状态检测时间查询")
    @ApiResponses({
            @ApiResponse(code = 200, message = "时间查询成功"),
            @ApiResponse(code = 202, message = "时间查询失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result login_check_select(@PathVariable int id){
        try{
            User user = userService.getById(id);
            if(user != null){
                return new Result(200, "查询成功", user.getCheckTime());
            }else {
                return new Result(202, "查询失败", "用户不存在");
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }

    @PutMapping("/user/login_check")
    @ApiOperation("登录状态检测时间更新")
    @ApiResponses({
            @ApiResponse(code = 200, message = "时间更新成功"),
            @ApiResponse(code = 202, message = "时间更新失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result login_check_update(int id){
        try{
            LocalDateTime localDateTime = LocalDateTime.now();
            User user = userService.getById(id);
            user.setCheckTime(localDateTime.toString());
            if(userService.updateById(user)){
                return new Result(200, "更新成功", user.getCheckTime());
            }else {
                return new Result(202, "时间更新失败", "时间更新失败");
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }

    @PostMapping("/user/login")
    @ApiOperation("账号密码效验")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 202, message = "查询失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result login(String username, String password){
        try{
            if(userService.login(username,password) != null){
                return new Result(200,"查询成功",userService.login(username,password));
            }else{
                return new Result(202,"账号或密码错误","账号：" + username + "密码：" + password);
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }

    @PostMapping("/user")
    @ApiOperation("新建")
    @ApiResponses({
            @ApiResponse(code = 200, message = "新建成功"),
            @ApiResponse(code = 202, message = "新建失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result add(User user){
        user.setLoginTime(null);
        try{
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", user.getUsername());
            if(userService.getOne(queryWrapper) != null){
                return new Result(202,"新建失败","用户名已被注册");
            }else{
                if (userService.save(user)) {
                    return new Result(200,"新建成功",user);
                }else{
                    return new Result(202,"新建失败",user);
                }
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数有误");
        }
    }

    @PutMapping("/user")
    @ApiOperation("修改")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 202, message = "修改失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result update(User user){
        user.setLoginTime(null);
        user.setBanTime(null);
        try{
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", user.getUsername());
            if(userService.getOne(queryWrapper) != null){
                queryWrapper.eq("id",user.getId());
                if(userService.getOne(queryWrapper) == null){
                    return new Result(202,"修改失败","用户名已被注册");
                }else {
                    if (userService.updateById(user)) {
                        return new Result(200,"修改成功",user);
                    }else{
                        return new Result(202,"修改失败",user);
                    }
                }
            }else {
                if (userService.updateById(user)) {
                    return new Result(200,"修改成功",user);
                }else{
                    return new Result(202,"修改失败",user);
                }
            }
        }catch (Exception e){
            e.getMessage();
            return new Result(400,"参数有误","请求参数有误");
        }
    }

    @PutMapping("/user/ban_time")
    @ApiOperation("修改禁止时间")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 202, message = "修改失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result updateBanTime(int id, String banTime){
        try{
            User user = null;
            if(userService.getById(id) != null){
                user = userService.getById(id);
                user.setBanTime(LocalDateTime.parse(banTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString());
                if(userService.updateById(user)){
                    user.setBanTime(banTime);
                    return new Result(200,"修改成功",user);
                }else {
                    return new Result(202,"修改失败","服务器出错");
                }
            }else{
                return new Result(202,"修改失败","用户不存在");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return new Result(400,"参数有误","请求参数有误");
        }
    }

    @DeleteMapping("/user/{id}")
    @ApiOperation("根据id删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 202, message = "删除失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result deleteById(@PathVariable int id){
        try{
            if(userService.removeById(id)){
                return new Result(200,"删除成功",id);
            }else {
                return new Result(202,"删除失败","id不存在");
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }
}

